package com.qumap.server;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.qumap.item.content.ObjectOnMapShort;
import com.qumap.item.test.iteminfo.item1.Ttem1UpdateProcessor;

/**
 * Created by IntelliJ IDEA.
 * User: Admin
 * Date: 28.05.11
 * Time: 19:09
 * To change this template use File | Settings | File Templates.
 */
public class Test1UpdateProcessor extends RemoteServiceServlet implements Ttem1UpdateProcessor {

    public static final double DELETE_FAULT_LIMIT = 0.05;
    public static final double UPDATE_FAULT_LIMIT = 0.05;

    private int item_index = 100;

    @Override
    public int save(ObjectOnMapShort item) {
        return getNextIndex();
    }

    @Override
    public void delete(int item_id) {
         if(!needDelete())
             throw new RuntimeException("Can't delete item");
    }

    @Override
    public void update(ObjectOnMapShort item) {
        if(!needUpdate())
            throw new RuntimeException("Can't update item");
    }

    protected int getNextIndex() {
        return item_index++;
    }

    protected boolean needDelete() {
        return checkLimit(DELETE_FAULT_LIMIT);
    }

    protected boolean needUpdate() {
        return checkLimit(UPDATE_FAULT_LIMIT);
    }

    private boolean checkLimit(double limit_value) {
        return Math.random() < (1-limit_value);
    }



}
